<?php
class Tools_page {
    public $center = 4;                     //第几个数开始翻页
    public $limit_page = 7;                 //一共显示几个页数
    public $page_template = 'page.html';    //page模板
    public $page_html;
    public $start = 0;
    /**
     * @param $total        总页数
     * @param $page_size    每页显示记录数
     * @param $page         当前页数
     * @param $para         参数
     * @return
     */
    public function  __construct($total,$page_size,$page,$para=array()) {
        if($total < 1) return 0;
        $this->para = (array)$para;
        $this->page = $page;
        $this->total = $total;
        $this->total_page = ceil($this->total / $page_size);
        $this->start = ($page - 1) * $page_size;
        $smarty = new Tools_Dismarty($this->page_template);
        $this->view['total_list'] = $total;
        $smarty->view['page'] = $this->setPage();
        $this->page_html = $smarty->getTemplate();
    }
    /**
     * 获得分页html代码
     * @return string
     */
    public function getPage() {
        return $this->page_html;
    }
    /**
     * 获得查询数据开始记录位置
     * @return int
     */
    public function getStart() {
        return $this->start;
    }
    /**
     * 计算分页
     * @return array
     */
    public function setPage() {
        $args = $this->para;
        $this->url = WEB_SITE.getRouter().'?';
        $this->page_content = array();
        $cut1 = $this->limit_page-$this->center;
        $cut2 = $this->center-$this->limit_page;
        if($this->page >=1) {
            $args['page'] = $this->page - 1;
            $nolink = $this->page==1 ?1:0;
            $this->page_content[] = $this->setArray('上一页',$this->url.http_build_query($args),$nolink);
        }
        if($this->page >= $this->center && $this->page+$cut1 <= $this->total_page) {
            $start = $this->page + $cut2;
            $end = $this->page + $cut1;
            $this->foreachPage($start,$end);
        } else if($this->page < $this->center && $this->total_page <= $this->limit_page) {
            $start = 1;
            $end = $this->total_page;
            $this->foreachPage($start,$end);
        } else if($this->page < $this->center && $this->total_page > $this->limit_page) {
            $start = 1;
            $end = $this->limit_page;
            $this->foreachPage($start,$end);
        } else if($this->page+$cut1>$this->total_page) {
            $start = $this->total_page - $this->limit_page + 1;
            $end = $this->total_page;
            $this->foreachPage($start,$end);
        }
        if($this->page <= $this->total_page) {
            $args['page'] = $this->page + 1;
            $nolink = $this->total_page==$this->page ?1:0;
            $this->page_content[] = $this->setArray('下一页',$this->url.http_build_query($args),$nolink);
            unset($end);
        }
//        if($start > 1) {
//            $args['page'] = 1;
//            array_unshift($this->page_content,$this->setArray('首页',$this->url.http_build_query($args),0));
//        }
//        if($end!=$this->total_page && $this->page!=$this->total_page) {
//            $args['page'] = $this->total_page;
//            $this->page_content[] = $this->setArray('末页',$this->url.http_build_query($args),0);
//        }
        return $this->page_content;
    }
    /**
     * 循环分页
     * @param  $start   开始页
     * @param  $end     结束页
     * @return array
     */
    public function foreachPage($start,$end) {
        $args = $this->para;
        for($i = $start; $i <= $end; $i++) {
            $args['page'] = $i;
            $nolink = $i==$this->page?1:0;
            $this->page_content[] = $this->setArray($i,$this->url.http_build_query($args),$nolink);
        }
    }
    /**
     * 设置分页上的链接
     * @param $name     链接名称
     * @param $link     链接
     * @param $nolink   是否有<a>标签 1|0 无|有
     * @return array
     */
    public function setArray($name,$link,$nolink=0) {
        return array('name' => $name, 'link' => $link, 'nolink' => $nolink);
    }
}
?>